package com.lijian.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lijian.mapper.MenuMapper;
import com.lijian.mapper.OrderMapper;
import com.lijian.mapper.TypeMapper;
import com.lijian.mapper.UserMapper;
import com.lijian.pojo.Menu;
import com.lijian.pojo.Order;
import com.lijian.pojo.OrderResult;
import com.lijian.pojo.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private UserMapper userMapper;

    @PostMapping("/save")
    public void save(@RequestBody Order order){
        orderMapper.insert(order);
    }

    @GetMapping("/findAllByUId")
    public OrderResult findAllByUid(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam Integer uid){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        IPage<Order> orderIPage = orderMapper.selectPage(new Page<>(page, limit), queryWrapper);
        Integer total = Math.toIntExact(orderIPage.getTotal());

        List<Order> orders = orderIPage.getRecords();
        orders.forEach(order -> {
            Menu menu = menuMapper.selectById(order.getMenuId());
            menu.setType(typeMapper.selectById(menu.getTypeId()));
            order.setMenu(menu);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:ss");
            order.setFormatDate(simpleDateFormat.format(order.getCreateTime()));
        });
        return new OrderResult(0, "成功", total, orders);
    }

    @GetMapping("/findAllByState")
    public OrderResult findAllByState(@RequestParam Integer page, @RequestParam Integer limit){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", 0);
        IPage<Order> orderIPage = orderMapper.selectPage(new Page<>(page, limit), queryWrapper);
        Integer total = Math.toIntExact(orderIPage.getTotal());

        List<Order> orders = orderIPage.getRecords();
        orders.forEach(order -> {
            Menu menu = menuMapper.selectById(order.getMenuId());
            menu.setType(typeMapper.selectById(menu.getTypeId()));
            order.setUser(userMapper.selectById(order.getUserId()));
            order.setMenu(menu);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:ss");
            order.setFormatDate(simpleDateFormat.format(order.getCreateTime()));
        });
        return new OrderResult(0, "成功", total, orders);
    }

    @PutMapping("/update/{id}")
    public void update(@PathVariable Integer id){
        Order order = new Order();
        order.setId(id);
        order.setState(1);
        orderMapper.updateById(order);
    }
}
